package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.page.PageResult;
import cn.wolfcode.crm.query.SaleChartQuery;
import cn.wolfcode.crm.query.ShowLossChartQuery;
import cn.wolfcode.crm.service.IChartService;
import com.alibaba.druid.sql.PagerUtils;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("chart")
public class ChartController {

    @Autowired
    private IChartService chartService;

    @RequestMapping("view")
    public String saleChats() {

        return "chart/salechart";

    }
    @RequestMapping("showloss")
    public String showLossChats() {

        return "chart/losschart";

    }

    @RequestMapping("list")
    @ResponseBody
    public PageResult list(SaleChartQuery qo) {
        PageResult result = new PageResult();
        List<Map<String, Object>> list = chartService.selectSaleChart(qo);

        return new PageResult(list.size(), list);

    }
    @RequestMapping("listloss")
    @ResponseBody
    public PageResult listloss(ShowLossChartQuery qo) {
        PageResult result = new PageResult();
        List<Map<String, Object>> list = chartService.selectListLossChart(qo);

        return new PageResult(list.size(), list);

    }

    @RequestMapping("listbyline")
    public String listbyline(Model model, SaleChartQuery qo) {
        List<Map<String, Object>> list = chartService.selectSaleChart(qo);
        //存储分组情况
        List<Object> groupTypeList = new ArrayList<>();

        //存储销售总额
        List<Object> amountList = new ArrayList<>();

        for (Map<String, Object> map : list) {

            groupTypeList.add(map.get("groupType"));
            amountList.add(map.get("saleAmount"));
        }
        model.addAttribute("groupTypeList", JSON.toJSONString(groupTypeList));
        model.addAttribute("amountList", JSON.toJSONString(amountList));
        model.addAttribute("groupType", SaleChartQuery.groupByTypes.get(qo.getGroupType()));
        return "chart/salechartbyline";
    }



    @RequestMapping("listlossbyline")
    public String listlossbyline(Model model, ShowLossChartQuery qo) {
        List<Map<String, Object>> list = chartService.selectListLossChart(qo);
        //存储分组情况
        List<Object> groupTypeList = new ArrayList<>();

        //存储销售总额
        List<Object> amountList = new ArrayList<>();

        for (Map<String, Object> map : list) {

            groupTypeList.add(map.get("groupType"));
            amountList.add(map.get("totalAccount"));
        }
        model.addAttribute("groupTypeList", JSON.toJSONString(groupTypeList));
        model.addAttribute("amountList", JSON.toJSONString(amountList));
        model.addAttribute("groupType", ShowLossChartQuery.groupByTypes.get(qo.getGroupType()));
        return "chart/losschartbyline";
    }

    @RequestMapping("listbypie")
    public String listbypie(Model model, SaleChartQuery qo) {
        List<Map<String, Object>> list = chartService.selectSaleChart(qo);
        //存储分组情况
        List<Object> groupTypeList = new ArrayList<>();

        //存储数据data
        List<Object> mapList = new ArrayList<>();

        //最大销售额
        BigDecimal maxSaleAmount = BigDecimal.ZERO;
        for (Map<String, Object> map : list) {

            groupTypeList.add(map.get("groupType"));
           Map<String,Object> mapData  = new HashMap<>();
            BigDecimal amount = (BigDecimal) map.get("saleAmount");
           if(amount.compareTo(maxSaleAmount)>0){
               maxSaleAmount = amount;
           }
            mapData.put("value",amount);
            mapData.put("name",map.get("groupType"));
            mapList.add(mapData);

        }
        model.addAttribute("groupTypeList", JSON.toJSONString(groupTypeList));
        model.addAttribute("maxSaleAmount", JSON.toJSONString(maxSaleAmount));
        model.addAttribute("mapList", JSON.toJSONString(mapList));
        model.addAttribute("groupType", SaleChartQuery.groupByTypes.get(qo.getGroupType()));
        return "chart/salechartbypie";
    }
    @RequestMapping("listlossbypie")
    public String listlossbypie(Model model, ShowLossChartQuery qo) {
        List<Map<String, Object>> list = chartService.selectListLossChart(qo);
        //存储分组情况
        List<Object> groupTypeList = new ArrayList<>();

        //存储数据data
        List<Object> mapList = new ArrayList<>();

        //最大销售额
        BigDecimal maxSaleAmount = BigDecimal.ZERO;
        for (Map<String, Object> map : list) {

            groupTypeList.add(map.get("groupType"));
           Map<String,Object> mapData  = new HashMap<>();
            BigDecimal amount = (BigDecimal) map.get("totalAccount");
           if(amount.compareTo(maxSaleAmount)>0){
               maxSaleAmount = amount;
           }
            mapData.put("value",amount);
            mapData.put("name",map.get("groupType"));
            mapList.add(mapData);

        }
        model.addAttribute("groupTypeList", JSON.toJSONString(groupTypeList));
        model.addAttribute("maxSaleAmount", JSON.toJSONString(maxSaleAmount));
        model.addAttribute("mapList", JSON.toJSONString(mapList));
        model.addAttribute("groupType", SaleChartQuery.groupByTypes.get(qo.getGroupType()));
        return "chart/losschartbypie";
    }

}
